package com.tianhui.consignortwofei.other.auth.recognition.recognitionResultProcess;

import android.text.TextUtils;

import com.fgs.common.utils.DateUtil;
import com.fgs.common.utils.LogUtil;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.enty.authLicense.VehicleLicenseInfo;
import com.tianhui.consignortwofei.mvp.model.enty.ocrResult.vehicleLicense.VehicleLicenseResult;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fgs
 * @date 2020/11/13
 * @description
 **/
public class VehicleLicenseOcrResultProcess {


    //百度ocr返回
    public VehicleLicenseInfo getVehicleOcrResult(VehicleLicenseResult vehicleLicenseResult, boolean isFront) {
        VehicleLicenseInfo vehicleLicenseInfo = new VehicleLicenseInfo();
        if (vehicleLicenseResult == null) {
            return vehicleLicenseInfo;
        }

        if (isFront) {
            /**
             * 正面
             */
            //车牌号
            if (vehicleLicenseResult.getPlatNumber() != null) {
                vehicleLicenseInfo.plateNo = vehicleLicenseResult.getPlatNumber().getWords();
                LogUtil.print("百度ocr返回车牌号", vehicleLicenseInfo.plateNo);

            }
            if (vehicleLicenseResult.getIssueDate() != null) {
                String date = vehicleLicenseResult.getIssueDate().getWords();
                //发证日期
                vehicleLicenseInfo.issueDate = dateFormat(date);
                LogUtil.print("百度ocr返回发证日期", date);
            }

            if (vehicleLicenseResult.getRegisterDate() != null) {
                //注册日期
                String date = vehicleLicenseResult.getRegisterDate().getWords();
                vehicleLicenseInfo.registerDate = dateFormat(date);
                LogUtil.print("百度ocr返回获取注册日期", date);
            }

            if (vehicleLicenseResult.getVehicleType() != null) {
                //车辆类型
                vehicleLicenseInfo.vehicleType = vehicleLicenseResult.getVehicleType().getWords();
            }
            if (vehicleLicenseResult.getBrandModel() != null) {
                //品牌型号
                vehicleLicenseInfo.model = vehicleLicenseResult.getBrandModel().getWords();
            }
            if (vehicleLicenseResult.getOwner() != null) {
                //所属人
                vehicleLicenseInfo.owner = vehicleLicenseResult.getOwner().getWords();
            }
            if (vehicleLicenseResult.getEngineNumber() != null) {
                //发动机号
                vehicleLicenseInfo.engineNo = vehicleLicenseResult.getEngineNumber().getWords();
            }
            if (vehicleLicenseResult.getIdentificationNumber() != null) {
                //车辆识别码
                vehicleLicenseInfo.vin = vehicleLicenseResult.getIdentificationNumber().getWords();
            }
            if (vehicleLicenseResult.getUseNature() != null) {
                //车辆使用性质
                vehicleLicenseInfo.useCharacter = vehicleLicenseResult.getUseNature().getWords();
            }
            if (vehicleLicenseResult.getUseNature() != null) {
                //发证机关
                vehicleLicenseInfo.seal = vehicleLicenseResult.getSeal().getWords();
            }
        } else {
            /**
             * 反面
             */
            if (vehicleLicenseResult.getOverallSizes() != null) {
                String overallSizes = vehicleLicenseResult.getOverallSizes().getWords();
                if (!TextUtils.isEmpty(overallSizes) && overallSizes.contains("mm")) {
                    Map<String, String> map = getSizeInfo(overallSizes);
                    if (map != null) {
                        try {

                            //长宽高
                            vehicleLicenseInfo.length = map.get(KeyValue.KEY_LENGTH);
                            LogUtil.print("长", vehicleLicenseInfo.length);
                            vehicleLicenseInfo.width = map.get(KeyValue.KEY_WIDTH);
                            LogUtil.print("宽", vehicleLicenseInfo.width);
                            vehicleLicenseInfo.height = map.get(KeyValue.KEY_HEIGHT);
                            LogUtil.print("高", vehicleLicenseInfo.height);

                            if (!TextUtils.isEmpty(vehicleLicenseInfo.length)) {
                                vehicleLicenseInfo.length = new BigDecimal(vehicleLicenseInfo.length).divide(new BigDecimal("1000"), 3, BigDecimal.ROUND_HALF_UP).toString();
                            }
                            if (!TextUtils.isEmpty(vehicleLicenseInfo.width)) {
                                vehicleLicenseInfo.width = new BigDecimal(vehicleLicenseInfo.width).divide(new BigDecimal("1000"), 3, BigDecimal.ROUND_HALF_UP).toString();
                            }
                            if (!TextUtils.isEmpty(vehicleLicenseInfo.height)) {
                                vehicleLicenseInfo.height = new BigDecimal(vehicleLicenseInfo.height).divide(new BigDecimal("1000"), 3, BigDecimal.ROUND_HALF_UP).toString();
                            }
                        }catch (Exception e){
                            LogUtil.print("长", vehicleLicenseInfo.length);
                        }

                    }


                }
            }

            //总质量
            String totalWeight = null;
            //整备质量
            String curbWeight = null;
            //核定载质量
            String approvedLoadCapacity = null;
            //准牵引总质量
            String tractionMass = null;

            if (vehicleLicenseResult.getTotalWeight() != null) {
                //总质量
                totalWeight = vehicleLicenseResult.getTotalWeight().getWords();
            }
            if (vehicleLicenseResult.getCurbWeight() != null) {
                //整备质量
                curbWeight = vehicleLicenseResult.getCurbWeight().getWords();
            }
            if (vehicleLicenseResult.getApprovedLoadCapacity() != null) {
                //核定载质量
                approvedLoadCapacity = vehicleLicenseResult.getApprovedLoadCapacity().getWords();
            }

            if (vehicleLicenseResult.getTractionMass() != null) {
                //准牵引总质量
                tractionMass = vehicleLicenseResult.getTractionMass().getWords();
            }
            vehicleLicenseInfo.loadQuality = approvedLoadCapacity;
            vehicleLicenseInfo.totalMass = totalWeight;
            vehicleLicenseInfo.curbWeight = curbWeight;
            vehicleLicenseInfo.totalQuasiMass = tractionMass;

            //核定载质量
            vehicleLicenseInfo.loadQuality = getApprovedLoadCapacity(vehicleLicenseInfo);
            //车辆总质量
            vehicleLicenseInfo.totalMass = getTotalWeight(vehicleLicenseInfo);


            //车牌号（挂）
            if (vehicleLicenseResult.getPlatNumber() != null) {
                LogUtil.print("百度ocr返回挂车车牌号" + vehicleLicenseResult.getPlatNumber().getWords());
                String plateNumber = vehicleLicenseResult.getPlatNumber().getWords();
                if (!plateNumber.contains("挂")) {
                    plateNumber = plateNumber + "挂";
                }
                vehicleLicenseInfo.plateNo = plateNumber;
            }
            //有效期
            if (vehicleLicenseResult.getIndate() != null) {
                String indate = vehicleLicenseResult.getIndate().getWords();
                vehicleLicenseInfo.indate = indate;
                LogUtil.print("百度ocr返回有效期：" + indate);
            }
        }

        return vehicleLicenseInfo;
    }

    public Map<String, String> getSizeInfo(String overallSizes) {
        Map<String, String> map = new HashMap<>();
        try {
            if (overallSizes.contains("mm")) {
                String[] strings = overallSizes.split("mm");
                String sizeString = strings[0];
                if (sizeString.contains("X")) {
                    String[] sizeStrings = sizeString.split("X");
                    if (sizeStrings.length == 3) {
                        //长宽高
                        map.put(KeyValue.KEY_LENGTH, sizeStrings[0]);
                        map.put(KeyValue.KEY_WIDTH, sizeStrings[1]);
                        map.put(KeyValue.KEY_HEIGHT, sizeStrings[2]);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public String dateFormat(String date) {
        return DateUtil.validDateFormat(date, "yyyy-MM-dd");
    }

    public String getFormatWeight(String weight) {
        String result = "";
        if (weight.contains("kg")) {
            result = weight.split("kg")[0];
        }
        return result;
    }

    //总质量
    public String getTotalWeight(VehicleLicenseInfo backInfo) {

        //总质量
        String totalWeight = "";
        //整备质量
        String curbWeight = "";
        //准牵引总质量
        String tractionMass = "";

        if (!TextUtils.isEmpty(backInfo.totalMass)) {
            totalWeight = getFormatWeight(backInfo.totalMass);
        }
        if (TextUtils.isEmpty(totalWeight)) {
            if (!TextUtils.isEmpty(backInfo.curbWeight)) {
                curbWeight = getFormatWeight(backInfo.curbWeight);
            }

            if (!TextUtils.isEmpty(backInfo.totalQuasiMass)) {
                //准牵引总质量
                tractionMass = getFormatWeight(backInfo.totalQuasiMass);
            }

            //如果总质量为空，则 总质量 = 整备质量+ 核定载质量
            if (!TextUtils.isEmpty(curbWeight) && !TextUtils.isEmpty(tractionMass)) {
                totalWeight = new BigDecimal(curbWeight).add(new BigDecimal(tractionMass)).toString();
            }
        }
        if (!TextUtils.isEmpty(totalWeight)) {
            totalWeight = new BigDecimal(totalWeight).divide(new BigDecimal("1000"), 3, BigDecimal.ROUND_HALF_UP).toString();
        }

        return totalWeight;
    }

    //核定载质量
    public String getApprovedLoadCapacity(VehicleLicenseInfo backInfo) {
        //核定载质量
        String approvedLoadCapacity = "";
        if (!TextUtils.isEmpty(backInfo.loadQuality)) {
            //核定载质量
            approvedLoadCapacity = getFormatWeight(backInfo.loadQuality);
        }
        if (TextUtils.isEmpty(approvedLoadCapacity)) {
            if (!TextUtils.isEmpty(backInfo.totalQuasiMass)) {
                //准牵引总质量
                approvedLoadCapacity = getFormatWeight(backInfo.totalQuasiMass);
            }
        }
        if (!TextUtils.isEmpty(approvedLoadCapacity)) {
            approvedLoadCapacity = new BigDecimal(approvedLoadCapacity).divide(new BigDecimal("1000"), 3, BigDecimal.ROUND_HALF_UP).toString();
        }

        return approvedLoadCapacity;
    }
}
